(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//时间转换
	function getMyDate(str){  
		var oDate = new Date(str),  
		oYear = oDate.getFullYear(),  
		oMonth = oDate.getMonth()+1,  
		oDay = oDate.getDate(),  
		oHour = oDate.getHours(),  
		oMin = oDate.getMinutes(),  
		oSen = oDate.getSeconds(),  
		oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间  
		return oTime;  
	}; 
	function getzf(num){  
          if(parseInt(num) < 10){  
              num = '0'+num;  
          }  
          return num;  
    }
	//获取下拉框类型
	function getlinklist(selectlist,selectid){
	   $.ajax({
			url:'http://web.icecn.net/base/resources/menuList.json',
			type:'GET',
			async:true,
			data:{
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.backResources;
					var d=new Array();
					if(dataset && dataset.length > 0){
					   var html = template(selectlist,{dataset:dataset});
					   $("#"+selectid).html(html);
					};
				}
			}
	   });	
	}
	//获取列表
	function getlist(curpage,pagenum){
		var menuId=$("#menuId").val()
		var resName=$("#resName").val();
	   $.ajax({
			url:'http://web.icecn.net/base/resources/resList.json',
			type:'get',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				menuId:menuId,
				resName:resName
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
						for(var i=0;i<dataset.length;i++){
							  
						}
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
						$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>")
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
			  curpage=1;
			  getlist(curpage,pagenum)
			})
			//删除按钮
			$(".content-wrapper").on("click", '.delete', function() {
				$("#modal-delete .modal-body").attr("data-id",$(this).attr("data-id"))
				$("#modal-delete .modal-body").attr("data-resourceName",$(this).attr("data-resourceName"))
			})
			//确认删除
			$(".btndelete").click(function(){
			   var id=$("#modal-delete .modal-body").attr("data-id");
			   var resourceName=$("#modal-delete .modal-body").attr("data-resourceName");
			   $.ajax({
					url:'http://web.icecn.net/base/resources/delete.json',
					type:'POST',
					async:true,
					data:{
						id:id,
						resourceName:resourceName
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-delete").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//上级目录更换
			$("#parentId").change(function(){
			   	if($(this).val()==0){
				  	$("#type").val(0);
				}else{
				  	$("#type").val(1);
				}
			})
			//新建修改按钮
			$(".content-wrapper").on("click", '.addupdate', function() {
				var id=$(this).attr("data-id");
				if(id!=null){
				  var obj=$(this).parent().parent().find("td");
				  $("#modal-addupdate .modal-title").html("修改");
				  $("#id").val(id);
				  $("#name").val(obj.eq(1).html());
				  $("#resKey").val(obj.eq(3).html());
				  $("#resUrl").val(obj.eq(4).html());
				  $("#parentId").val($(this).attr("data-parentId"));
				  $("#type").val(obj.eq(2).html()=="目录"?"0":"1");
				  $("#level").val(obj.eq(5).html());
				  $("#description").val(obj.eq(7).html());
				}else{
				  $("#modal-addupdate .modal-title").html("新建")
				  $("#id").val("");
				  $("#name").val("");
				  $("#resKey").val("");
				  $("#resUrl").val("");
				  $("#parentId").val("0");
				  $("#type").val("0");
				  $("#level").val("");
				  $("#description").val("");
				}
			})
			//确认修改按钮
			$(".btnaddupdate").click(function(){
				 var id=$("#id").val();
				 var name=$("#name").val();
				 var resKey=$("#resKey").val();
				 var resUrl=$("#resUrl").val();
				 var parentId=$("#parentId").val();
				 var type=$("#type").val();
				 var level=$("#level").val();
				 var description=$("#description").val();
				 if(name==""){alert("请输入资源名称");return}
				 if(id>0){
					 $.ajax({
						url:'http://web.icecn.net/base/resources/save.json',
						type:'POST',
						async:true,
						data:{
							id:id,
							name:name,
							resKey:resKey,
							resUrl:resUrl,
							parentId:parentId,
							type:type,
							description:description,
							level:level
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert(data.msg)	
							}
						}
				     });
				 }else{
					 $.ajax({
						url:'http://web.icecn.net/base/resources/save.json',
						type:'POST',
						async:true,
						data:{
							name:name,
							resKey:resKey,
							resUrl:resUrl,
							parentId:parentId,
							type:type,
							description:description,
							level:level
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert(data.msg)	
							}
						}
				     });
				 }
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum);
			getlinklist("selectlist","menuId");
			getlinklist("selectlist2","parentId");
		}
	};
	Page.init();
})();